package com.web.page;

import java.util.HashMap;
import java.util.Map;

import com.web.annotations.Copy;
import com.web.annotations.PageElement;
import com.web.annotations.PageUrl;
import com.web.email.EmailParser;
import com.web.imp.AbstractWebPage;
import com.web.model.element.WebButton;
import com.web.model.element.WebTextField;
import com.web.lang.Language;
import com.web.lang.LanguageConfig;

@PageUrl("/user/forgotstep1/")
public class ForgotPassword extends AbstractWebPage{

	public static final String EMAIL_LINK_URL = "/user/forgotstep3/key/";
	
	public static final String RESTORE_PASSWORD_EMAIL_TEMPLATE = LanguageConfig.getLanguage() + "-restore_pass.html";
	
	@Copy(id = "title_login")
	public static String TITLE_COPY;
	
	@Copy(id = "text_remming")
	public static String EXPLAINATION_COPY;
	
	@Copy(id = "email")
	public static String EMAIL_COPY;
	
	@Copy(id = "email_confirm")
	public static String CONFIRM_EMAIL_COPY;
	
	@Copy(id = "send")
	public static String SEND_COPY;
	
	@Copy(id = "back")
	public static String BACK_COPY;
	
	@Copy(id = "mensaje_recordar_pass_paso2")
	public static String EMAIL_SENT_COPY;

	@Copy(id = "isEmpty")
	public static String BLANK_FIELDS_COPY;
	
	@Copy(id = "emailAddressInvalid")
	public static String INVALID_EMAIL_COPY;
	
	@Copy(id = "forgot_password_change_pass")
	public static String FORGOT_EMAIL_SUBJECT;
	
	
	@PageElement(id = "email")
	private WebTextField emailField;
	
	@PageElement(id = "email2")
	private WebTextField email2Field;
	
	@PageElement(id = "add_login")
	private WebButton sendButton;
	
	
	public static String getEmailSentCopy(String email){
		
		return EMAIL_SENT_COPY.replaceAll("%email%", email);
	}
	
	public static String getInvalidEmailCopy(String invalidEmail){
		
		return INVALID_EMAIL_COPY.replace("%value%", invalidEmail);
	}
	
	public static String getTitle(){
		
		return TITLE_COPY + " - " + Index.TITLE;
	}
	
	public static String getRecoverPasswordEmailTemplate(String email, String url){
		
		Map<String, String>	values = new HashMap<String, String>();
		values.put("user_name", email);
		values.put("url", url);
		
		return new EmailParser(RESTORE_PASSWORD_EMAIL_TEMPLATE).getParsedContent(values);
	}

	
	public Login sendForgotPasswordEmail(String email){
		
		emailField.sendKeys(email);
		email2Field.sendKeys(email);
		
		sendButton.click();
		
		return browser.goTo(Login.class);
	}
	
	public void leaveEmailBlank(String email) {
		
		emailField.sendKeys("");
		email2Field.sendKeys(email);
		
		sendButton.click();
	}
	
	public void leaveConfirmEmailBlank(String email) {
		
		emailField.sendKeys(email);
		email2Field.sendKeys("");
		
		sendButton.click();
	}
	
	public void leaveFieldsBlank() {
		
		emailField.sendKeys("");
		email2Field.sendKeys("");
		
		sendButton.click();
	}

	public String getUrl(){
		
		Language language = LanguageConfig.getLanguage();
						
		return this.url + "?lang=" + language;
	}

	public void setInvalidEmail(String invalidEmail) {
		
		emailField.sendKeys(invalidEmail);
		email2Field.sendKeys(invalidEmail);
		
		sendButton.click();
	}
}
